<?PHP

class ads
{
	
	var $post;
	var $where;
	var $data;
	var $input;
	var $output;
	var $files;
	var $mode;
	var $zone;
	var $playlist;
	var $category;
	var $img;
	
	
	function serve_image_ad($mode)
	{
	
		$ad_media=current($this->ad_chooser());
		global $conf;
		if(!empty($ad_media))
		{
			
			$this->post['zone']=$ad_media['zone'];
			$this->post['campaign']=$ad_media['campaign'];
			$this->post['ad_media']=$ad_media['ad_media'];
			$this->post['url']=$conf->canonical;
			$this->impress();
			if(empty($mode)){
			
			?>
				<a href='ads?link=<?php echo $ad_media['link'] ?>'><img src='<?PHP echo ap($ad_media['image']);?>'></img></a>
			<?PHP
			}
			else{
		
			 $this->img = "<img src=".ap($ad_media['image'])." height=235 width=280></img>";
		
			}
		}
		
	}
	
	function impress()
	{	
		global $conf;
		
		if($conf->advertise)
		
		{
			$this->post['date']=date("Y-m-d H:i:s",time());
			process_sql_post($this->post,'ad_impression','add',NULL,NULL);
		}
		
	}
	function get_campaign()
	{
		
		$this->data=get_sql('ad_campaign','id',$where,NULL);
		
		
	}
	
	function get_zone()
	{
		$this->data=get_sql('ad_zone','id',$where,NULL);
	}
	
	function get_media()
	{
		$this->data=get_sql('ad_media','id',$where,NULL);
	}
	
	function ad_chooser()
	{	
		
		
		
		//GET ZONES
		
			$w[0]='zone';
			$w[1]=$this->zone;
			$w[2]='=';
			$where[]=$w;
			$w[0]='start';
			$w[1]=date("Y-m-d",time());
			$w[2]='<=';
			$where[]=$w;
			$w[0]='end';
			$w[1]=date("Y-m-d",time());
			$w[2]='>=';
			$where[]=$w;
			$w[0]='impressions_current';
			$w[1]=0;
			$w[2]='>';
			$where[]=$w;
			$o['order'][0]='priority';
			$o['order'][1]='DESC';
			
			
			$w[0]='category';
			$w[1]=0;
			$w[2]='=';
			$where[]=$w;
			
			
			$ij[0]=TRUE;
			$ij[1]='media';
			$ij[2][0]='server';
			$ij[2][1]='file';
			$ij[2][2]='image';
			$ij[2][3]='thumb';
			$ij[2][4]='image_url';
			$ij[2][5]='image';
			$ij[3][0]='media';
			$ij[3][1]='id';
		
			$ad_media=get_sql('ad_media','id',$where,$o,$ij);
		//	$ad_media=get_sql('ad_media','id',$where,$o);
			
			
			unset($where);
			
			$w[0]='zone';
			$w[1]=$this->zone;
			$w[2]='=';
			$where[]=$w;
			$w[0]='start';
			$w[1]=date("Y-m-d",time());
			$w[2]='<=';
			$where[]=$w;
			$w[0]='end';
			$w[1]=date("Y-m-d",time());
			$w[2]='>=';
			$where[]=$w;
			$w[0]='impressions_current';
			$w[1]=0;
			$w[2]='>';
			$where[]=$w;
			$w[0]='category';
			$w[1]=$this->category;
			$w[2]='=';
			$where[]=$w;
			
			$ad_media_category=get_sql('ad_media','id',$where,$o,$ij);
			
			
			foreach($ad_media as $x)
			{
				//GET MEDIA
				
				unset($where);
				$w[0]='id';
				$w[1]=$x['media'];
				$w[2]='=';
				$where[]=$w;
				$m=get_sql('media','id',$where,NULL);
				$v=current($m);
				$y=array_merge((array)$v,(array)$x);
				
				$this->playlist[]=$y;
			}
			
			if(!empty($ad_media_category))
			{
				
				
				unset($this->playlist);
				foreach($ad_media_category as $x)
				{
					//GET MEDIA
					
					unset($where);
					$w[0]='id';
					$w[1]=$x['media'];
					$w[2]='=';
					$where[]=$w;
					$m=get_sql('media','id',$where,NULL);
					$v=current($m);
					$y=array_merge((array)$v,(array)$x);

					$this->playlist[]=$y;
				}
				
				
			}
			
	
		//GET CAMPAIGN
		
			shuffle($this->playlist);
			return $this->playlist;
		
	}
	function get_audio_lineup()
	{
		
		
	}
}
?>
